package com.thh.shirodemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thh.shirodemo.bean.Deal;
import com.thh.shirodemo.bean.Goods;
import com.thh.shirodemo.bean.User;
import com.thh.shirodemo.service.DealService;
import com.thh.shirodemo.service.DealServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author AdrianHHT
 * @create 2022-07-30-16:07
 */
@RequiresPermissions("salary")
@Controller
public class DealController {
    @Autowired
    DealService dealService;
    /**
     * 分页查找 目前用户 的订单情况
     */
    @RequestMapping("/lookDeals")
    public String lookDeals(@RequestParam(value="pn",defaultValue = "1") Integer pn,
                            Model model,
                            HttpSession session
    ){
        Subject subject = SecurityUtils.getSubject();
        User currentUser = (User)subject.getSession().getAttribute("currentUser");
        //获取当前对象的id
        Integer userId = currentUser.getUserId();
        //分页
        IPage<Deal> page = new Page<>(pn,5);
        QueryWrapper<Deal> queryWrapper = new QueryWrapper<>();
        //显示出该用户的商品
        queryWrapper.eq("user_id",userId);
        IPage<Deal> dealPage = dealService.page(page, queryWrapper);
        if(dealPage.getTotal()==0){
            session.setAttribute("dealMsg","目前你还没有参加任何活动哦");
        }else{
            session.setAttribute("dealMsg","以下是你的参与的活动订单:");
        }
        model.addAttribute("deals",dealPage);
        return "CurrentDeal";
    }
}
